package com.earth.c.plugins.jfinal;

import java.sql.Connection;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;

//@Configuration
public class JFinalActiveRecordPluginConfig {
	
	
	@Value("${spring.mysql.druid.dsname}")
	private String dsname;
	
	@Value("${spring.mysql.druid.url}")
	private String url;

	@Value("${spring.mysql.druid.username}")
	private String username;

	@Value("${spring.mysql.druid.password}")
	private String password;

	@Value("${spring.mysql.druid.showSql}")
	private boolean showSql;
	
	@Value("${spring.mysql.druid.initialSize}")
	private int initialSize;
	
	@Value("${spring.mysql.druid.minIdle}")
	private int minIdle;
	
	@Value("${spring.mysql.druid.maxActive}")
	private int maxActive;
	

	@Bean
	public ActiveRecordPlugin initActiveRecordPlugin() {
		
		DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
		druidPlugin.setInitialSize(initialSize);
		druidPlugin.setMinIdle(minIdle);
		druidPlugin.setMaxActive(maxActive);

		// 加强数据库安全
		WallFilter wallFilter = new WallFilter();
		wallFilter.setDbType("mysql");

		druidPlugin.addFilter(wallFilter);
		// 添加 StatFilter 才会有统计数据
		druidPlugin.addFilter(new StatFilter());
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dsname,druidPlugin);
		
		arp.setShowSql(showSql);
		arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
		arp.getEngine().setToClassPathSourceFactory();
		arp.addSqlTemplate("/sql/all_sqls.sql");
		//com.yth.supplychain.common.mapping._MappingKit.mapping(arp);
		arp.setDialect(new MysqlDialect());
		druidPlugin.start();
		arp.start();
		return arp;
	}

}
